﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_FileListBll.cs
文件名称：FileListBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010/12/17
作　　者：
内容摘要：表[File_List]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.IO;
using Eis51.CampanyCMS.Model;
using System.Data;
using System.Collections;
using System.Web.UI.HtmlControls;
using Eis51.CampanyCMS.FrameWork;
using System.Web.UI.WebControls;
using System.Web;
using Eis51.CampanyCMS.Access.Bus;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
	/// FileList的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class FileListBLL : BaseBLL<FileList>
    {

        public static string SaveFile(HtmlInputFile UploadFile, string foder)
        {
            string sss = UploadFile.PostedFile.FileName;
            if (string.IsNullOrEmpty(sss)) return string.Empty;
            FileInfo info = new FileInfo(sss);
            if (!Directory.Exists(foder))
            {
                Directory.CreateDirectory(foder);
            }
            string fileName = sss;//DateTime.Now.Ticks.ToString() + info.Extension;
            // string patch = Server.MapPath("~/Img/UpLoadFile/");
            UploadFile.PostedFile.SaveAs(Path.Combine(foder, fileName));
            return fileName;
        }

        public static string SaveProductImg(HtmlInputFile UploadFile, bool HaveSmall)
        {
            string sss = UploadFile.PostedFile.FileName;
            string BigImg = HttpContext.Current.Server.MapPath(AppContext.UserFileFoder + "/BigImg");
            string SmallImg = HttpContext.Current.Server.MapPath(AppContext.UserFileFoder + "/smallimg");
            if (string.IsNullOrEmpty(sss)) return string.Empty;
            FileInfo info = new FileInfo(sss);
            if (!Directory.Exists(BigImg))
            {
                Directory.CreateDirectory(BigImg);
            }
            if (!Directory.Exists(SmallImg))
            {
                Directory.CreateDirectory(SmallImg);
            }
            string fileName = AppContext.CurrentAdministrator.UserName + DateTime.Now.Ticks.ToString() + info.Extension;
            string BigFileName = Path.Combine(BigImg, fileName);
            UploadFile.PostedFile.SaveAs(Path.Combine(BigImg, fileName));
            if (HaveSmall)
            {
                CreateSmallImage.MakeSPic(140, 140, BigFileName, Path.Combine(SmallImg, fileName));
            }
            return fileName;
        }


        public static string SaveFile(FileUpload control, string foder)
        {
            string sss = control.PostedFile.FileName;
            if (string.IsNullOrEmpty(sss)) return string.Empty;
            FileInfo info = new FileInfo(sss);
            if (!Directory.Exists(foder))
            {
                Directory.CreateDirectory(foder);
            }
            string fileName = AppContext.CurrentAdministrator.UserName + DateTime.Now.Ticks.ToString() + info.Extension;
            control.PostedFile.SaveAs(Path.Combine(foder, fileName));
            return fileName;
        }

        public string SaveFile(HtmlInputFile UploadFile)
        {
            string sss = UploadFile.PostedFile.FileName;
            if (sss == string.Empty) return string.Empty;
            FileInfo info = new FileInfo(sss);
            string patch = string.Format(HttpContext.Current.Server.MapPath("~/UpLoadFile/{0}/img/"), AppContext.CurrentAdministrator.Createby);
            string fileName = FileListBLL.SaveFile(UploadFile, patch);
            return fileName;
        }

        public static string ShowDownloadFile(string Finame)
        {
            return "../UpLoadFile/download/" + Finame;
        }
        public static string ShowBigImg(string Finame)
        {
            return "../UpLoadFile/admin/Bigimg/" + Finame;
        }

        public static string ShowSmallImg(string Finame)
        {
            return "../UpLoadFile/admin/Small/" + Finame;
        }

        #region  MyData

        /// <summary>
        /// 通过产品ID取得文件
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ArrayList GetFilesByProductId(string productId, FileType type)
        {
            string sql = "select * from File_List where key_value={0} and Key_type='{1}'";
            if (productId == "0")
            {
                sql += " and 1<>1";
            }

            sql = string.Format(sql, productId, ((int)type).ToString());
            sql = sql + " order by Sequence ";
            DataTable dt = this.GetDataTable(sql);
            return FileList.Dt2Objs(dt, FileList.ColumnNameEnum.DBName) as ArrayList;
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="imgFileName"></param>
        /// <param name="fileListId"></param>
        /// <returns></returns>
        public bool DeleteProductImg(string imgFileName, string fileListId)
        {
            DeleteProducImg(imgFileName);
            if (fileListId != "" && fileListId != "0")
            {
                string sql = "delete from File_List where seqno = " + fileListId;
                this.Execute(sql);
            }
            return true;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool DeleteFile(string fileName)
        {
            string finame = HttpContext.Current.Server.MapPath(AppContext.UserDownloadFileFoder + "download/" + fileName);
            if (File.Exists(finame))
            {
                File.Delete(finame);
            }
            return true;
        }

        public void DeleteProducImg(string Filename)
        {
            string finame = HttpContext.Current.Server.MapPath(AppContext.UserFileFoder + "bigimg/" + Filename);
            if (File.Exists(finame))
            {
                File.Delete(finame);
            }
            finame = HttpContext.Current.Server.MapPath(AppContext.UserFileFoder + "smallimg/" + Filename);
            if (File.Exists(finame))
            {
                File.Delete(finame);
            }
        }

        /// <summary>
        /// 删除更新时不在产品里的图片
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="fileLists"></param>
        /// <returns></returns>
        public bool ProductImgSubMit(string productId, IList fileLists, FileType type)
        {
            //对比数据库的图片
            #region 删除有在现有列表里的图
            string haslistids = "";
            foreach (FileList tmp in fileLists)
            {
                if (tmp.Seqno != 0)
                {
                    haslistids += "{0},";
                    haslistids = string.Format(haslistids, tmp.Seqno);
                }
            }
            string sql = "select * from File_List where key_value={0} and Key_type='{1}'";
            sql = string.Format(sql, productId, ((int)type).ToString(), haslistids.Trim(','));
            if (haslistids.Trim(',') != "")
            {
                sql += " and seqno not in({0})";
                sql = string.Format(sql, haslistids.Trim(','));
            }
            DataTable dt = this.GetDataTable(sql);

            IList it = FileList.Dt2Objs(dt, FileList.ColumnNameEnum.DBName);

            foreach (FileList tmp in it)
            {
                DeleteProducImg(tmp.FileName);
            }
            sql = "delete from File_List where key_value={0} and Key_type='{1}'";
            sql = string.Format(sql, productId, ((int)type).ToString(), haslistids.Trim(','));
            if (haslistids.Trim(',') != "")
            {
                sql += " and seqno not in({0})";
                sql = string.Format(sql, haslistids.Trim(','));
            }
            this.Execute(sql);
            #endregion

            foreach (FileList tmp in fileLists)
            {
                if (tmp.Seqno == 0)
                {
                    tmp.KeyType = ((int)type).ToString();
                    tmp.KeyValue = int.Parse(productId);
                    this.Insert(tmp);
                }
                else
                {
                    this.Update(tmp);
                }
            }
            return true;
        }




        #endregion
    }


    #region 代码自动生成
    /// <summary>
    /// FileList的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class FileListBLL : BaseBLL<FileList>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: FileListBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected FileListBLL()
        {
        }
        private static volatile FileListBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 FileListBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static FileListBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(FileListBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new FileListBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>FileList</returns>
        public FileList Get(FileList.Key key)
        {

            return FileListDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return FileListDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(FileList obj)
        {
            int result = 0;
            try
            {
                result = FileListDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(FileList obj)
        {
            FileListDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(FileList obj)
        {
            bool result = FileListDao.GetInstance().DeleteByID(new FileList.Key(obj.Seqno));
            return result ? 1 : 0;
        }

        #endregion
    }
    #endregion
}